import 'package:flutter/material.dart';
import 'package:flutter_module/base/model/constants.dart';
import 'package:flutter_module/base/util/sp_util.dart';
import 'package:flutter_module/util/log_u.dart';
import 'package:flutter_module/util/theme/custom_theme.dart';
import 'package:flutter_module/util/language_res/str_res_keys.dart';
import 'package:get/get.dart';

class ThemeSettingsController extends GetxController {

  var currentThemeIndex = 0;

  List<String> themeList = [
    SR.followerSystemTheme.tr,
    SR.lightTheme.tr,
    SR.darkTheme.tr,
    SR.greenTheme.tr,
    SR.orangeTheme.tr,
    SR.purpleTheme.tr,
    SR.pinkTheme.tr,
    SR.tealTheme.tr,
  ];

  @override
  void onInit() {
    super.onInit();
    //使用Get.find的方式，onInit方法仅调用一次
    var result = SharedPreferencesU.getInstance().get(Constants.theme);
    LogU.log("参数信息内容查看一下 result = $result");
    currentThemeIndex = SharedPreferencesU.getInstance().get(Constants.theme) ?? 0;
    update();
  }

  ///切换主题
  void changeTheme(BuildContext context, int themeIndex) {
    LogU.log("参数信息内容查看一下 themeIndex = $themeIndex");

    ThemeData themeData;
    switch (themeIndex) {
      case 0: //跟隨系統
        themeData = MediaQuery.of(context).platformBrightness == Brightness.dark ? darkTheme : lightTheme;
        break;
      case 1: //浅色模式
        themeData = lightTheme;
        break;
      case 2: //深色模式
        themeData = darkTheme;
        break;
      case 3:
        themeData = greenTheme;
        break;
      case 4:
        themeData = orangeTheme;
        break;
      case 5:
        themeData = purpleTheme;
        break;
      case 6:
        themeData = pinkTheme;
        break;
      case 7:
        themeData = tealTheme;
        break;
      default:
        themeData = MediaQuery.of(context).platformBrightness == Brightness.dark ? darkTheme : lightTheme;
        break;
    }

    currentThemeIndex = themeIndex;
    update();

    //保存到本地
    SharedPreferencesU.getInstance().setData(Constants.theme,themeIndex);
    Get.changeTheme(themeData);
  }
}